<?php

namespace App\Http\Controllers;

use Illuminate\Foundation\Bus\DispatchesJobs;
use Illuminate\Routing\Controller as BaseController;
use Illuminate\Foundation\Validation\ValidatesRequests;
use Illuminate\Foundation\Auth\Access\AuthorizesRequests;
use Illuminate\Http\UploadedFile;
use Illuminate\Support\Str;
use Illuminate\Support\Facades\Storage;

class Controller extends BaseController
{
    use AuthorizesRequests, DispatchesJobs, ValidatesRequests;

    protected function uploadToCloudinary(UploadedFile $uploadedFile)
    {
        $disk = config('admin.upload.disk');
        $path = config('admin.upload.directory.image');
        $uploadResult = Storage::disk($disk)->putFileAs($path, $uploadedFile, Str::random());
        return $uploadResult;
    }

    public function cloudinaryToken()
    {
        $disk = 'cloudinary';
        $config = config('filesystems.disks.' . $disk);
        $timestamp = time();
        $signature = \Cloudinary::api_sign_request([
            'timestamp' => $timestamp,
        ], $config['api_secret']);

        return response()->json(api(0, MSG_OK, [
            'disk' => $disk,
            'upload_url' => sprintf('https://api.cloudinary.com/v1_1/%s/image/upload', $config['cloud_name']),
            'api_key' => $config['api_key'],
            'signature' => $signature,
            'timestamp' => $timestamp,
        ]));
    }
}
